#
# Frunk Makefile
#

SVNREV="$Revision: 6 $"

VERSION=0.1.1

DESTDIR=/usr/local/frunk

PKGFILES=CHANGES LICENSE README Makefile scanner.l parser.y \
		*.c *.h frunkd.conf.sample

INSTALL=install

DEFINES=-DVERSION_FRUNKD=\"$(VERSION)\" -DDEFAULT_FRUNKD_DIR=\"$(DESTDIR)\"

COMMON_CFLAGS=-Wall -pedantic $(DEFINES)

CFLAGS=$(DEBUG) $(COMMON_CFLAGS)
#CFLAGS=$(OPTIMIZATION) $(COMMON_CFLAGS)

DEBUG=-g

OPTIMIZATION=-O2

YACC=bison -y
YFLAGS=-d

LEX=flex -l

LIBS=-lsnmp -lm

SRCS=config.c dllist.c log.c mailmsg.c main.c parser.c \
	pollstat.c report.c scanner.c snmpapi.c utils.c

OBJS=$(SRCS:.c=.o)

.PHONY: all
all: frunkd

frunkd: $(OBJS)
	$(LINK.c) $(OBJS) $(LIBS) -o $@

parser.c parser.h: parser.y
	$(YACC.y) parser.y
	mv -f y.tab.c parser.c
	mv -f y.tab.h parser.h

scanner.c: scanner.l parser.h
	$(RM) $@
	$(LEX.l) $< > $@

%.d: %.c
	$(SHELL) -ec '$(CC) -MM $(CPPFLAGS) $< \
		| sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
		[ -s $@ ] || rm -f $@'

.PHONY: clean
clean:
	rm -rf .package
	rm -f parser.c parser.h scanner.c *.d *.o frunkd

.PHONY: package
package: clean
	mkdir -p .package/frunk-$(VERSION)
	cp $(PKGFILES) .package/frunk-$(VERSION)/
	tar -C .package -czvf .package/frunk-$(VERSION).tar.gz \
		frunk-$(VERSION)

.PHONY: install
install: frunkd
	$(INSTALL) -d $(DESTDIR)
	$(INSTALL) -s frunkd $(DESTDIR)
	$(INSTALL) -m 644 frunkd.conf.sample $(DESTDIR)
	$(INSTALL) -m 644 README $(DESTDIR)
	$(INSTALL) -m 644 LICENSE $(DESTDIR)

-include $(SRCS:.c=.d)
